package com.lemner.motion.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by hzq on 2018/3/5.
 */

public class DataSQLOpenHelper extends SQLiteOpenHelper {

    // 建表命令
    public static final String CREATE_TABLE_RUN =
            "create table sporttable " +
                    "(id integer primary key,"+"loacationarray text," +
                    "colorarray text,"+"usetime integer,"+
                    "distance real)" ;

    // 单例
    private static DataSQLOpenHelper dataSQLOpenHelper = null;
    // 获得可写的数据库对象
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public static DataSQLOpenHelper getCustomSQLOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {

        synchronized (DataSQLOpenHelper.class) {

            if (dataSQLOpenHelper == null) {
                dataSQLOpenHelper = new DataSQLOpenHelper(context, name, factory, version);
            }
        }

        return dataSQLOpenHelper;

    }
    // 构造方法
    public DataSQLOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
        // 获取可写的数据库对象
        this.sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_RUN);
        Toast.makeText(context, "建表", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    //插入数据库
    public void insertValue(int id, List<String> locationarray, List<String> colorarray, int usetime, double distance) {
       String location="",color="";
        for (int i=0;i<locationarray.size();i++){
            if (i==0)
                location+=locationarray.get(i);
            else {
                location+="-";
                location+=locationarray.get(i);
            }
        }
        for (int i=0;i<colorarray.size();i++){
            if (i==0)
                color+=colorarray.get(i);
            else {
                color+="-";
                color+=colorarray.get(i);
            }
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", id);
        contentValues.put("loacationarray", location);
        contentValues.put("colorarray", color);
        contentValues.put("usertime", usetime);
        contentValues.put("distance", distance);
        long num=sqLiteDatabase.insert("sporttable", null, contentValues);
        Log.i("item","item"+num);
    }
    public ArrayList<DataBean> selectValues() {
        //Cursor - 游标
        Cursor cursor = sqLiteDatabase
                .rawQuery("select * from adverttable",null );
        //只要cursor能移动到下一条
        //我们就持续的从cursor中拿数据
        ArrayList<DataBean> data = new ArrayList<DataBean>();
        while (cursor.moveToNext()) {
            DataBean res = new DataBean();
            List<String>locationarray,colorarray;
            res.setId(cursor.getInt(cursor.getColumnIndex("id")));

            locationarray= Arrays.asList(cursor.getString(cursor.getColumnIndex("loacationarray")).split("-"));
            colorarray=Arrays.asList(cursor.getString(cursor.getColumnIndex("colorarray")).split("-"));
           res.setColorarray(colorarray);
            res.setLocationarray(locationarray);
            res.setDistance(cursor.getDouble(cursor.getColumnIndex("distance")));
            res.setUsetime(cursor.getInt(cursor.getColumnIndex("usetime")));
            data.add(res);
        }
        cursor.close();
        return data;
    }
}
